#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#define SOUND_ZOOM  "/weapons/hunting_rifle/gunother/hunting_rifle_zoom.wav"
 
new bool:Enable[MAXPLAYERS+1];
new bool:ZoomOn[MAXPLAYERS+1];
new KeyBuffer[MAXPLAYERS+1];
new fovmin=10;
new fovmax=90;
new String:weapon[32];
new middle=45;
new button;
 
public Plugin:myinfo = 
{
	name = "ZOOM",
	author = "xiaohai",
	description = "ZOOM",
	version = "1.0",
	url = "http://forums.alliedmods.net"
}
public OnPluginStart()
{
 	AutoExecConfig(true, "l4d_zoom");	
 	HookEvent("player_spawn", Event_Spawn);
	HookEvent("weapon_zoom", weapon_zoom);
	HookEvent("weapon_reload", WeaponReload);
	RegConsoleCmd("sm_zoominc", zoom1);
 	RegConsoleCmd("sm_zoomdec", zoom2);
	
	RegConsoleCmd("sm_zoombind", sm_zoombind);
	RegConsoleCmd("sm_zoom", sm_zoom);
	
}
 
public OnMapStart()
{  
	PrecacheSound(SOUND_ZOOM, true) ;
}
public OnMapEnd()
{
 	
}
 
public WeaponReload (Handle:hEvent, const String:name[], bool:dontBroadcast)
{
	new client  = GetClientOfUserId(GetEventInt(hEvent, "userid")); 
	if(Enable[client]==false)return;
	if(IsValidAliveClient(client))
	{
 		SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
		SetEntProp(client,    Prop_Send, "m_iFOV", 90);
	}
	ZoomOn[client]=false;
}
public PlayerConnectFull(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid")); 
	Enable[client]=true;
}
 
public Event_Spawn(Handle:Spawn_Event, const String:Spawn_Name[], bool:Spawn_Broadcast)
{
	new client = GetClientOfUserId(GetEventInt(Spawn_Event, "userid"));
	if(Enable[client]==false)return;
	if(IsValidAliveClient(client))
	{
		SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
		SetEntProp(client,    Prop_Send, "m_iFOV", 90); 
	}
	ZoomOn[client] = false;

}
public Action:sm_zoombind(client, args)
{
	Enable[client]=true;
	ClientCommand(client, "bind mwheelup \"say /zoominc\"");
	ClientCommand(client, "bind mwheeldown \"say /zoomdec\"");
	return Plugin_Handled;
}
public Action:sm_zoom(client, args)
{
	Enable[client]=!Enable[client];
	if(Enable[client])
	{
		PrintToChat(client, "zoom enabled");
	}
	return Plugin_Handled;
}
public Action:zoom1(client, args)
{
 	if (!IsValidAliveClient(client))return Plugin_Handled;
	if(Enable[client]==false)return Plugin_Handled;
	Zoom(client, -15);
	return Plugin_Handled;
}
public Action:zoom2(client, args)
{
	if (!IsValidAliveClient(client))return Plugin_Handled;
	if(Enable[client]==false)return Plugin_Handled;
	Zoom(client, 15);
	return Plugin_Handled;
}
Zoom(client, step)
{
	new fov;
 
	GetClientWeapon(client, weapon, 32);
 	if(StrEqual(weapon, "weapon_hunting_rifle") ||  StrContains(weapon, "sniper")>=0)
	{
		fov=GetEntProp(client,  Prop_Send, "m_iFOV");
 		if(fov==0)fov=90;
		fov+=step;
		if(fov>fovmax)fov=fovmax;
		if(fov<fovmin)fov=fovmin;
 		SetEntProp(client,    Prop_Send, "m_iFOV", fov);
 		ZoomOn[client]=false;
	}
	else
	{
 		fov=GetEntProp(client,  Prop_Send, "m_iFOV");
 		if(fov==0)fov=90;
		fov+=step;
		if(fov>fovmax)fov=fovmax;
		if(fov<fovmin)fov=fovmin;
		if(fov==90)
		{
			SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
		}
		else
		{
			SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  0);
		}
		SetEntProp(client,    Prop_Send, "m_iFOV", fov);
 		ZoomOn[client]=true;
	}
	EmitSoundToAll(SOUND_ZOOM, client); 
 }


public weapon_zoom(Handle:Spawn_Event, const String:Spawn_Name[], bool:Spawn_Broadcast)
{
	
	new client = GetClientOfUserId(GetEventInt(Spawn_Event, "userid"));
	if(Enable[client]==false)return;
	
	new String:weapon[32];
	GetClientWeapon(client, weapon, 32);
	new fov=GetEntProp(client,  Prop_Send, "m_iFOV");
	new view=GetEntProp(client,    Prop_Send, "m_bDrawViewmodel"); 
	if(StrEqual(weapon, "weapon_hunting_rifle"))
	{
		if(fov!=30)
		{
			SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
			SetEntProp(client,    Prop_Send, "m_iFOV", 0);
			EmitSoundToAll(SOUND_ZOOM, client); 
		}
		//PrintToChatAll("weapon_hunting_rifle %d, %d", fov, view);		
		return;
	}
	else if(StrEqual(weapon, "weapon_sniper_military"))
	{
		if(fov!=30)
		{
			SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
			SetEntProp(client,    Prop_Send, "m_iFOV", 0);
			EmitSoundToAll(SOUND_ZOOM, client); 
		}
		//PrintToChatAll("weapon_sniper_military %d, %d", fov, view);	
		ZoomOn[client]=false;return;
	}
		else if(StrEqual(weapon, "weapon_sniper_awp"))
	{
		if(fov!=40)
		{
			SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
			SetEntProp(client,    Prop_Send, "m_iFOV", 0);
			EmitSoundToAll(SOUND_ZOOM, client); 
		}
		//PrintToChatAll("weapon_sniper_awp %d, %d", fov, view);	
		ZoomOn[client]=false;return;
	}
	else if(StrEqual(weapon, "weapon_sniper_scout"))
	{
		if(fov!=40)
		{
			SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
			SetEntProp(client,    Prop_Send, "m_iFOV", 0);
			EmitSoundToAll(SOUND_ZOOM, client); 
		}
		//PrintToChatAll("weapon_sniper_scout %d, %d", fov, view);	
		ZoomOn[client]=false;return;
	}
	return;
	 
	
 }
/*
public OnGameFrame()
{
  	for(new client = 1; client <= MaxClients; client++)
	{
*/
public Action:OnPlayerRunCmd(client, &buttons, &impuls, Float:vel[3], Float:angles[3], &w)
{
	if(Enable[client]==false)return;
	if(IsClientInGame(client) && IsPlayerAlive(client) && GetClientTeam(client)==2 && !IsFakeClient(client))
	{
		button=GetClientButtons(client);
		if((button & IN_ZOOM) && !(KeyBuffer[client] & IN_ZOOM))
		{
			GetClientWeapon(client, weapon, 32);
			if(! (StrEqual(weapon, "weapon_hunting_rifle") || StrContains(weapon, "sniper")>=0))
			{
				if(ZoomOn[client])
				{
					SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
					SetEntProp(client,    Prop_Send, "m_iFOV", 90);
					ZoomOn[client]=false;
				}
				else
				{
					SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  0);
					SetEntProp(client,    Prop_Send, "m_iFOV", middle);
					ZoomOn[client]=true;
				}
				EmitSoundToAll(SOUND_ZOOM, client); 
			}
		}
		if(ZoomOn[client])
		{
			if(button & IN_ATTACK2)
			{
				SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
				SetEntProp(client,    Prop_Send, "m_iFOV", 90);
				ZoomOn[client]=false;
			}
			else if(!(button & IN_DUCK) && !(button & IN_SPEED))
			{
				if(button & IN_FORWARD)
				{
					SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
					SetEntProp(client,    Prop_Send, "m_iFOV", 90);
					ZoomOn[client]=false;
				}
				else if(button & IN_MOVELEFT)
				{
					SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
					SetEntProp(client,    Prop_Send, "m_iFOV", 90);
					ZoomOn[client]=false;
				}
				else if(button & IN_MOVERIGHT)
				{
					SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
					SetEntProp(client,    Prop_Send, "m_iFOV", 90);
					ZoomOn[client]=false;
				}
				else if(button & IN_BACK)
				{
					SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
					SetEntProp(client,    Prop_Send, "m_iFOV", 90);
					ZoomOn[client]=false;
				}
				//else if(button & IN_JUMP)
				//{
					//SetEntProp(client,    Prop_Send, "m_bDrawViewmodel",  1);
					//SetEntProp(client,    Prop_Send, "m_iFOV", 90);
					//ZoomOn[client]=false;
				//}
			}

		}
		KeyBuffer[client]=button;
	}
	 
}
SetMove(client, Float:v)
{
	 SetEntDataFloat(client, offsSpeed, v);
}
 stock bool:IsValidAliveClient(iClient)
{
    if (iClient <= 0) return false;
    if (iClient > MaxClients) return false;
    if(!IsClientInGame(iClient))return false;
    if (GetClientTeam(iClient)!=2) return false;
    if (!IsPlayerAlive(iClient)) return false;
    if (IsFakeClient(iClient)) return false;
	return true;
}
